package cms.simmytech.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cms.simmytech.response.BaseResponse;
import cms.simmytech.util.ResultCode;
import cms.simmytech.util.StringUtils;
import cms.simmytech.util.UploadUtil;
import cms.simmytech.web.base.BaseController;

@Controller
@RequestMapping(value = "/common")
public class CommonController extends BaseController {

	@Autowired
	private UploadUtil uploadUtil;

	@Value("${ueditor.config.root}")
	private String root;

	@PostMapping(value = "/upload")
	@ResponseBody
	public Map<String, Object> upload(MultipartFile file) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (file.isEmpty()) {
			map.put("res", false);
		} else {
			String url = uploadUtil.uploadFile(file);

			map.put("res", true);
			map.put("url", url);
		}
		return map;
	}

	@PostMapping(value = "/upload_file")
	@ResponseBody
	public Map<String, Object> uploadFile(MultipartFile file, byte type, @RequestParam(value = "savePostion", defaultValue = "") String savePostion) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("stat", ResultCode.SERVER_ERROR);
		if (!file.isEmpty()) {
			if (StringUtils.isEmpty(savePostion)) {
				BaseResponse response = uploadUtil.uploadFile(file, type);
				if (null != response) {
					map.put("res", response.getStat());
					map.put("url", response.getMess());
				}
			} else {
				BaseResponse response = uploadUtil.uploadFile(file, type, savePostion);
				if (null != response) {
					map.put("res", response.getStat());
					map.put("url", response.getMess());
				}
			}
		}
		return map;
	}

	@RequestMapping(value = "/exec")
	@ResponseBody
	public Object exec(String action, String nocache, MultipartFile file, String type, String name, String id, String[] source,
	        HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			response.setContentType("application/json;charset=utf-8");
			if (action.equals("config")) {
				File f = new File(root + "/config.json");
				BufferedReader br = new BufferedReader(new FileReader(f));
				StringBuilder stringBuilder = new StringBuilder();
				String line;
				while ((line = br.readLine()) != null) {
					stringBuilder.append(line);
				}
				return stringBuilder.toString();
			} else if (action.equals("upload")) {
				Map<String, Object> msg = upload1(file, "ueditor/");
				msg.put("state", "SUCCESS");
				msg.put("original", file.getOriginalFilename());
				msg.put("size", file.getSize());
				msg.put("title", name);
				msg.put("type", type);
				msg.put("code", 0);
				msg.put("url", this.webPath + msg.get("url"));
				msg.put("id", id);
				return msg;
			} else if (action.equals("catchimage")) {
				if (source != null && source.length > 0) {
					for (String img : source) {
						File f = uploadUtil.getHttpImage(img);
					}
				}
				Map<String, Object> msg = upload1(file, "ueditor/");
				msg.put("state", "SUCCESS");
				msg.put("original", file.getOriginalFilename());
				msg.put("size", file.getSize());
				msg.put("title", name);
				msg.put("type", type);
				msg.put("code", 0);
				msg.put("url", this.webPath + msg.get("url"));
				msg.put("id", id);
				return msg;
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	public Map<String, Object> upload1(MultipartFile file, String root) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (file.isEmpty()) {
			map.put("res", false);
		} else {
			String url = uploadUtil.uploadFile(file, root);
			map.put("res", true);
			map.put("url", url);
		}
		return map;
	}

}
